package com.example.bankend.controllers;

import com.example.bankend.entity.Card;
import com.example.bankend.entity.Trade;
import com.example.bankend.form.*;
import com.example.bankend.param.Result;
import com.example.bankend.service.CardService;
import com.example.bankend.service.TradeService;
import com.example.bankend.util.Validator;
import com.example.bankend.vo.CardResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author luolinila
 * @date 2025/6/18 14:52
 */
@CrossOrigin
@RestController
public class TradeController {
    @Autowired
    private TradeService tradeService;
    @Autowired
    private CardService cardService;

    @PostMapping("/trade/withdraw")
    public Result<CardResult> withdraw(@RequestBody Withdraw form) {
        boolean inputTure = form.isTure();
        if (!inputTure) {
            return new Result<>(10000, "输入错误", null);
        }
        boolean isCardID = Validator.is16DigitNumber(form.getCardID());
        if (!isCardID) {
            return new Result<>(10001, "银行卡号格式错误", null);
        }
        if (form.getMoney().compareTo(new BigDecimal(0)) <= 0 || form.getMoney().compareTo(new BigDecimal(50000)) > 0 || form.getMoney().compareTo(new BigDecimal("0.01")) < 0) {
            return new Result<>(10002, "输入金额错误", null);
        }
        boolean isPass = Validator.isCardPass(form.getPass());
        if (!isPass) {
            return new Result<>(10003, "密码格式错误", null);
        }
        String id = tradeService.Withdraw(form.getCardID(), form.getMoney(), form.getPass());
        if ("10004".equals(id) || "10005".equals(id)) {
            return new Result<>(10003, "密码错误", null);
        }
        if ("10006".equals(id)) {
            return new Result<>(10004, "余额不足", null);
        }
        if ("-1".equals(id)) {
            return new Result<>(10005, "取款失败", null);
        }
        Card card = cardService.getCardByID(form.getCardID());
        if (card == null) {
            return new Result<>(10006, "未知错误", null);
        }
        CardResult res = cardService.CardEncapsulaReturns(card);
        if (res == null) {
            return new Result<>(10006, "未知错误", null);
        }
        return new Result<>(200, "取款成功", res);
    }

    @PostMapping("/trade/saveMoney")
    public Result<CardResult> saveMoney(@RequestBody SaveMoney form) {
        boolean inputTure = form.isTure();
        if (!inputTure) {
            return new Result<>(10000, "输入错误", null);
        }
        boolean isCardID = Validator.is16DigitNumber(form.getCardID());
        if (!isCardID) {
            return new Result<>(10001, "银行卡号格式错误", null);
        }
        if (form.getMoney().compareTo(new BigDecimal(0)) <= 0 || form.getMoney().compareTo(new BigDecimal(50000)) > 0 || form.getMoney().compareTo(new BigDecimal("0.01")) < 0) {
            return new Result<>(10002, "输入金额错误", null);
        }
        boolean isPass = Validator.isCardPass(form.getPass());
        if (!isPass) {
            return new Result<>(10003, "密码格式错误", null);
        }
        String id = tradeService.saveMoney(form.getCardID(), form.getMoney(), form.getPass());
        if ("10004".equals(id) || "10005".equals(id)) {
            return new Result<>(10003, "密码错误", null);
        }
        if ("-1".equals(id)) {
            return new Result<>(10005, "存款失败", null);
        }
        Card card = cardService.getCardByID(form.getCardID());
        if (card == null) {
            return new Result<>(10006, "未知错误", null);
        }
        CardResult res = cardService.CardEncapsulaReturns(card);
        if (res == null) {
            return new Result<>(10006, "未知错误", null);
        }
        return new Result<>(200, "存款成功", res);
    }

    @PostMapping("/trade/transfer")
    public Result<CardResult> transfer(@RequestBody Transfer form) {
        boolean inputTure = form.isTure();
        if (!inputTure) {
            return new Result<>(10000, "输入错误", null);
        }
        boolean isCardID = (Validator.is16DigitNumber(form.getFromCardID()) && Validator.is16DigitNumber(form.getToCardID()));
        if (!isCardID) {
            return new Result<>(10001, "银行卡号格式错误", null);
        }
        if (form.getMoney().compareTo(new BigDecimal(0)) <= 0 || form.getMoney().compareTo(new BigDecimal(50000)) > 0 || form.getMoney().compareTo(new BigDecimal("0.01")) < 0) {
            return new Result<>(10002, "输入金额错误", null);
        }
        boolean isPass = Validator.isCardPass(form.getPass());
        if (!isPass) {
            return new Result<>(10003, "密码格式错误", null);
        }
        String id = tradeService.transfer(form.getFromCardID(), form.getToCardID(), form.getMoney(), form.getPass());
        if("10004".equals(id) ) {
            return new Result<>(10003, "银行卡不存在", null);
        }
        if ("10005".equals(id)) {
            return new Result<>(10003, "密码错误", null);
        }
        if ("10006".equals(id)) {
            return new Result<>(10004, "余额不足", null);
        }
        if ("-1".equals(id)) {
            return new Result<>(10005, "转账失败", null);
        }
        Card card = cardService.getCardByID(form.getFromCardID());
        if (card == null) {
            return new Result<>(10006, "未知错误", null);
        }
        CardResult res = cardService.CardEncapsulaReturns(card);
        if (res == null) {
            return new Result<>(10006, "未知错误", null);
        }
        return new Result<>(200, "转账成功", res);
    }

    @PostMapping("trade/delete")
    private Result<CardResult> deleteTrade(@RequestBody DeleteTrade form) {
        boolean inputTure = form.isTure();
        if (!inputTure) {
            return new Result<>(10000, "输入错误", null);
        }
        Long res = tradeService.deleteTrade(form.getTradeID(), form.getAdminID(), form.getAdminName(), form.getAdminPass());
        if (res == -1) {
            return new Result<>(10005, "删除失败", null);
        }
        if (res == 10004) {
            return new Result<>(10004, "记录不存在", null);
        }
        return new Result<>(200, "删除成功", null);
    }


    @PostMapping("/trade/selectAll")
    public Result<List<Trade>> selectAllTrade(@RequestBody SelectAllTrade form) {
        boolean inputTure = form.isTure();
        if (!inputTure) {
            return new Result<>(10000, "输入错误", null);
        }
        List<Trade> res = tradeService.selectAllTrade(form.getAdminID(), form.getAdminName(), form.getAdminPass(), form.getCustomerID());
        if (res == null) {
            return new Result<>(10005, "查询失败", null);
        }
        return new Result<>(200, "查询成功", res);
    }

    @PostMapping("/trade/getAllTrade")
    public Result<List<Trade>> getAllTrade(@RequestBody SelectAll form) {
        boolean inputTure = form.isTure();
        if (!inputTure) {
            return new Result<>(10000, "输入错误", null);
        }
        List<Trade> res = tradeService.getAllTrade(form.getAdminID(), form.getAdminName(), form.getAdminPass());
        if (res == null) {
            return new Result<>(10001, "查询失败", null);
        }
        return new Result<>(200, "查询成功", res);
    }
}